package com.stagewebserver.webserver.service.impl;


import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stageuploadserver.uploadserver.pojo.UploadResponse;
import com.stageuploadserver.uploadserver.utils.MinioUtil;
import com.stagecommon.common.mapper.UserMapper;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.pojo.User;
import com.stagewebserver.webserver.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yxl
 * @since 2021-11-09
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    MinioUtil minioUtil;

    @Override
    public RespBean<Integer> userRegister(User user) {
        if (!Objects.equals(user.getCode(), redisTemplate.opsForValue().get("regCode" + user.getEmail()))) {
            return RespBean.operationFail("验证码错误！");
        } else if (userMapper.selectCount(new QueryWrapper<User>().select("username").eq("username", user.getUsername())) == 1) {
            return RespBean.operationFail("用户名已存在!");
        }
        user.setPassword(SaSecureUtil.md5(user.getPassword()));
        user.setUserImg("http://124.220.168.106:9001/stage/img/userAvatar/defaultUserImg.jpg");
        return RespBean.operationSuccess("注册成功", 200, userMapper.insert(user));
    }

    @Override
    public RespBean forgetPwd(User user) {
        if (!Objects.equals(user.getCode(), redisTemplate.opsForValue().get("forgetPwd" + user.getEmail()))) {
            return RespBean.operationFail("验证码错误！");
        } else if (!Objects.equals(null, userMapper.selectOne(new QueryWrapper<User>()
                .select("email")
                .eq("email", user.getEmail())))) {
            userMapper.update(null, new UpdateWrapper<User>().eq("email", user.getEmail()).set("password", SaSecureUtil.md5(user.getPassword())));
            return RespBean.operationSuccess("更改密码成功！", 200, "ok");
        }
        return RespBean.operationFail("用户不存在！");
    }

    @Override
    public RespBean<User> selectUser(User user) {
        return RespBean.operationSuccess("查询成功", 200, userMapper.selectById(user));
    }

    @Override
    public RespBean<String> changePwd(User user) {
        if (!Objects.equals(user.getCode(), redisTemplate.opsForValue().get("changePwd" + user.getEmail()))) {
            return RespBean.operationFail("验证码错误！");
        } else if (!Objects.equals(null, userMapper.selectOne(new QueryWrapper<User>().select("email").eq("email", user.getEmail())))) {
            userMapper.update(user, new UpdateWrapper<User>().eq("email", user.getEmail()).set("password", SaSecureUtil.md5(user.getPassword())));
            return RespBean.operationSuccess("修改密码成功", 200, "ok");
        }
        return RespBean.operationFail("用户邮箱不存在！");
    }

    @Override
    public RespBean updateUserInfo(User user) {
        if ((!Objects.equals(user.getOldUsername(), user.getUsername())) && userMapper.selectCount(new QueryWrapper<User>().eq("username", user.getUsername())) > 0) {
            return RespBean.operationFail("用户名已存在");
        }
        userMapper.update(user, new UpdateWrapper<User>().eq("user_id", user.getUserId()));
        return RespBean.operationSuccess("保存成功", 200, "ok");
    }

    @Override
    public RespBean<Integer> updateAvatar(MultipartFile avatar, User user) throws Exception {

        String originalFilename = avatar.getOriginalFilename();
        assert originalFilename != null;
        String fileName = "img/userAvatar/" + originalFilename;

        UploadResponse bucket01 = minioUtil.uploadFile(avatar, "stage", fileName);

        user.setUserImg(bucket01.getMinIoUrl());

        if (userMapper.updateById(user) == 1) {
            return RespBean.operationSuccess("更新成功", 200, 1);
        }
        return RespBean.operationSuccess("更新失败", -10, 0);
    }

    @Override
    public RespBean selectUserImg(User user) {
        return RespBean.operationSuccess("查找成功", 200, userMapper.selectUserImg(user.getUsername()));
    }

}
